package main

import (
	"github.com/BurntSushi/toml"
	"log"
	"path/filepath"
	"strings"
)

type config struct {
	Server struct {
		Addr string
	}
	Log struct {
		Console struct {
			Use      bool
			LogLevel int
		}
		File struct {
			Use      bool
			LogLevel int
			Name     string
		}
	}
	Supervisor map[string]projSetting
}

type projSetting struct {
	Commands []command
}

type command struct {
	Name     string
	Cmd      string
	Outlog   string
	Path     string `toml:"workDir"`
	PushData bool
	Order    int
}

var cfg = new(config)

func loadConfig() {
	_, err := toml.DecodeFile(*configPath, cfg)
	if err != nil {
		log.Fatalln("open config error:", err)
	}
	normalize(cfg)
}

func normalize(cfg *config) {
	cfg.Log.File.Name = filepath.Clean(cfg.Log.File.Name)

	for _, v := range cfg.Supervisor {

		for _, u := range v.Commands {

			u.Outlog = filepath.Clean(strings.TrimSpace(u.Outlog))
			abspath, err := filepath.Rel(wd, u.Outlog)

			if err == nil {
				u.Outlog = abspath
			}
			u.Path = filepath.Clean(strings.TrimSpace(u.Outlog))
			abspath, err = filepath.Rel(wd, u.Path)
			if err == nil {
				u.Path = abspath
			}

		}

	}
}
